package com.bailian.spring.ioc.basic_factorybean.config;

import com.bailian.spring.ioc.basic_factorybean.bean.Ball;
import com.bailian.spring.ioc.basic_factorybean.bean.Car;
import com.bailian.spring.ioc.basic_factorybean.bean.Child;
import com.bailian.spring.ioc.basic_factorybean.bean.Toy;
import org.springframework.beans.factory.FactoryBean;

/**
 * @author gaoxu
 * date: Created on 2021/11/1 下午1:35
 */

public class ToyFactoryBean implements FactoryBean<Toy> {

    private Child child;

    public void setChild(Child child) {
        this.child = child;
    }

    public ToyFactoryBean() {
        System.out.println("初始化factoryBean.....");
    }

    @Override
    public Toy getObject() throws Exception {
        switch (child.getWantToy()) {
            case "ball":
                return new Ball("ball");
            case "car":
                return new Car("car");
            default:
                // SpringFramework2.0开始允许返回null,之前的1.x版本是不允许的
                return null;
        }
    }

    @Override
    public Class<?> getObjectType() {
        return Toy.class;
    }
}
